package com.mjk.common.core.jdbc.ibatis.typehandler.handler;

import com.mjk.common.core.jdbc.ibatis.typehandler.BaseHandler;
import com.mjk.common.util.ObjectUtils;
import com.mjk.common.util.TimeUtils;

import java.lang.reflect.Field;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * 时间格式为 HH:mm:ss
 */
public class LocalTimeHandler implements BaseHandler<LocalTime> {
    @Override
    public String resovleToString(Field f, LocalTime localTime) {
        if (ObjectUtils.isEmpty(localTime)) {
            return null;
        }
        return localTime.format(DateTimeFormatter.ofPattern(TimeUtils.DATE_FORMAT_TIME));
    }

    @Override
    public LocalTime resovleResult(Object o) {
        return LocalTime.ofInstant(TimeUtils.parse(o.toString()).toInstant(), ZoneId.systemDefault());
    }
}
